import { http } from './request'

/**
 * 用户相关API
 */
export const userApi = {
    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     */
    login: (username: string, password: string) => {
        return http.post<{
            token: string
            userInfo: {
                id: string
                username: string
                avatar: string
                nickname: string
            }
        }>('/user/login', { username, password })
    },

    /**
     * 获取用户信息
     */
    getUserInfo: () => {
        return http.get<{
            id: string
            username: string
            avatar: string
            nickname: string
        }>('/user/info')
    },

    /**
     * 用户登出
     */
    logout: () => {
        return http.post('/user/logout')
    }
}

/**
 * 通用API
 */
export const commonApi = {
    /**
     * 上传文件
     * @param file 文件对象
     */
    uploadFile: (file: File) => {
        return new Promise<{ url: string }>((resolve, reject) => {
            const uploadTask = uni.uploadFile({
                url: import.meta.env.VITE_UPLOAD_BASEURL,
                filePath: file,
                name: 'file',
                success: res => {
                    if (res.statusCode === 200) {
                        const data = JSON.parse(res.data)
                        resolve(data)
                    } else {
                        reject(res)
                    }
                },
                fail: err => {
                    reject(err)
                }
            })
        })
    }
}
